layui.define('fly', function (exports) {

    var $ = layui.jquery;
    var layer = layui.layer;
    var util = layui.util;
    var laytpl = layui.laytpl;
    var form = layui.form;
    var fly = layui.fly;
    var element = layui.element;

    var gather = {}, dom = {
        jieda: $('#jieda')
        , content: $('#L_content')
        , jiedaCount: $('#jiedaCount')
    };

    //监听专栏选择
    form.on('select(column)', function (obj) {
        var value = obj.value
            , elemQuiz = $('#LAY_quiz')
            , tips = {
            tips: 1
            , maxWidth: 250
            , time: 10000
        };
        elemQuiz.addClass('layui-hide');
        if (value === '0') {
            layer.tips('科技新潮带你领略新时代的style', obj.othis, tips);
            elemQuiz.removeClass('layui-hide');
        } else {
            layer.tips('这里可以再写一些目的的主要功能', obj.othis, tips);
        }
    });


    // 留言提交监听
    form.on('submit(Replay001)', function (data) {

        var action = $(data.form).attr('action'), button = $(data.elem);
        //同步获取 用户 信息
        lucode.getUserInfoSyn(function (userInfo) {
            //console.log(userInfo);
            var tplData = {
                "nickname": userInfo.nickname,
                "avatar": userInfo.avatar,
                "userId": userInfo.userId,
                "content": data.field.content

            };
            // 同步发起 post 请求  增加留言
            lucode.lucodePost(lucode.serverURL + action, data.field, function (res) {
                if (res != false) {
                    var tpl = '<li>\
                               <div class="detail-about detail-about-reply">\
                                <a class="fly-avatar" href="#" target="_blank">\
                                 <img src="{{= d.avatar}}" alt="{{= d.nickname}}">\
                                </a>\
                              <div class="fly-detail-user">\
                                   <a href="#" target="_blank" class="fly-link">\
                                <cite>{{d.nickname}}</cite>\
                                  </a>\
                              </div>\
                             <div class="detail-hits"><span>刚刚</span></div></div>\
                             <div class="detail-body jieda-body photos">{{ d.content}}</div></li>';

                    laytpl(tpl).render(tplData, function (html) {
                        $('#jieda').find('.fly-none').remove();
                        // 将渲染后的  视图填充到页面
                        $('#jieda').append(html);
                        //情况  填充框
                        $("#L_content").val('');
                    });
                }
            });
        });
        return false;
    });


    // 赞
    form.on('submit(zan)', function (data) {

        var action = $(data.form).attr('action');
        // 同步发起 post 请求  增加留言
        lucode.lucodePost(lucode.serverURL + action, data.field, function () {
            // id标签 是  zanCount+replyId
            // 赞 +1 操作
            var dom=document.getElementById("zanCount"+ data.field.replyId);
            var count =parseInt(dom.innerText);
            dom.innerText = ++count;
            // 修改图标为已赞状态
            dom=document.getElementById("zanIcon"+ data.field.replyId).getAttribute('class');
            dom=dom.concat(" zanok");
            document.getElementById("zanIcon"+ data.field.replyId).setAttribute('class',dom);

        });
        return false;
    });

    // 提交 众筹信息
    form.on('submit(addPost)', function (data) {

        var action = $(data.form).attr('action');
        lucode.getUserInfoSyn(function (userInfo) {
            var req=data.field;
            req.userId=userInfo.userId;

            // 同步发起 post 请求  增加留言
            lucode.lucodePost(lucode.serverURL + action, req, function (res) {

                layer.confirm('发布成功，点击查看', {
                    btn: ['确认'] //可以无限个按钮
                }, function(index, layero){
                    window.parent.location.href="/lucode-for-all-web/cms/index.html?postId="+res.data.id
                });

            });

        });
        return false;

    });








    $('body').on('click', '.jie-admin', function () {
        var othis = $(this), type = othis.attr('type');
        gather.jieAdmin[type] && gather.jieAdmin[type].call(this, othis.parent());
    });


    $('.jieda-reply span').on('click', function () {
        var othis = $(this), type = othis.attr('type');
        gather.jiedaActive[type].call(this, othis.parents('li'));
    });


    //定位分页
    if (/\/page\//.test(location.href) && !location.hash) {
        var replyTop = $('#flyReply').offset().top - 80;
        $('html,body').scrollTop(replyTop);
    }




    // 文章 载入
    $(document).ready(function () {
        // 加载正文页面
        var url = window.location.href;
        if (url.toString().indexOf("detail")) {
            // 取出 url 中 的 postId
            var postId = lucode.getStringByRegular(url);
            lucode.lucodeGet(lucode.serverURL + "/cmsPost/get/" + postId,
                null, null,
                function (data) {

                    if(data.CmsPost != null){
                        // 将数据 整合起来 封装到 data 里面
                        data.CmsPost.gmtCreate = lucode.formatDate(data.CmsPost.gmtCreate);
                        data.CmsPost.gmtModify = lucode.formatDate(data.CmsPost.gmtModify);

                        if (data.CmsPost.isStick == "on") {
                            data.CmsPost.isStick = "<span class=\"layui-badge layui-bg-black\">置顶</span>";
                        } else {
                            data.CmsPost.isStick = "";
                        }
                        if (data.CmsPost.state == "on") {
                            data.CmsPost.state = "<span class=\"layui-badge\" style=\"background-color: #5FB878;\">众筹中</span>";
                        } else {
                            data.CmsPost.state = "<span class=\"layui-badge\" style=\"background-color: #999;\">众筹结束</span>";
                        }

                        // 留言部分时间的格式化处理
                        for (var i = 0; i < data.ReplyList.length; i++) {
                            data.ReplyList[i].gmtCreate = lucode.formatDate(data.ReplyList[i].gmtCreate);
                        }
                        // 获取整个模板引擎
                        var detailTpl = document.getElementById("detail").innerHTML,
                            // 获取 渲染后的视图 存放位置
                            view = document.getElementById('view');
                        laytpl(detailTpl).render(data, function (html) {
                            // 渲染视图
                            view.innerHTML = html;
                        });
                    }
                });
        }
        // 参与众筹
        $("#join").click(function(){

            var html="<div style=\"padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;\">" +
                "<div class=\"layui-carousel\" id=\"test1\">\n" +
                "  <div carousel-item>\n" +
                "    <div><img style=\"height: 310px ;width: 285px\" src=\"http://lucode-for-all-1251927515.file.myqcloud.com/201803131507385280000010003004081520924365284.jpg\"/></div>\n" +
                "    <div><img style=\"height: 310px ;width: 285px\" src=\"http://lucode-for-all-1251927515.file.myqcloud.com/201803131507385280000010003004081520924365284.jpg\"/></div>\n" +
                "    <div><img style=\"height: 310px ;width: 285px\" src=\"http://lucode-for-all-1251927515.file.myqcloud.com/201803131507385280000010003004081520924365284.jpg\"/></div>\n" +
                "    <div><img style=\"height: 310px ;width: 285px\" src=\"http://lucode-for-all-1251927515.file.myqcloud.com/201803131507385280000010003004081520924365284.jpg\"/></div>\n" +
                "    <div><img style=\"height: 310px ;width: 285px\" src=\"http://lucode-for-all-1251927515.file.myqcloud.com/201803131507385280000010003004081520924365284.jpg\"/></div>\n" +
                "  </div>\n" +
                "</div></div>";


            layer.open({
                type: 1
                ,title: "参与众筹" //不显示标题栏
                ,closeBtn: false
                ,area: ['400px','450px']
                ,shade: 0.8
                ,id: 'lay001' //设定一个id，防止重复弹出
                ,resize: false
                ,btn: ['火速参与', '残忍拒绝']
                ,btnAlign: 'c'
                ,moveType: 1 //拖拽模式，0或者1
                ,content: html
                ,success:function () {
                    layui.use('carousel', function(){
                        var carousel = layui.carousel;
                        //建造实例
                        carousel.render({
                            elem: '#test1'
                            ,width: '100%' //设置容器宽度
                            ,arrow: 'always' //始终显示箭头
                        });
                    });
                }
                ,yes:function (index, layero) {
                    //同步获取 用户 信息
                    lucode.getUserInfoSyn(function (userInfo) {
                        var postId=document.getElementById("postId").innerHTML;
                        console.log(document.getElementById("postId"));
                        var data = {
                            "userId": userInfo.userId,
                            "postId": postId,
                            "cash": "0.01"

                        };
                        layer.confirm('如果没有扫码支付，本次交易交失败', {
                            btn: ['先扫码','已扫码'], //按钮
                            time: 2000
                        }, function(){

                        }, function(){
                            lucode.lucodePost(lucode.serverURL + "/cmsCash/addOne", data, function (res) {
                                layer.close(index);
                                if(res.resCode==lucode.SUCCESS){
                                    layer.msg('参与成功,感谢,请在用户中心查看参与情况');
                                } else {
                                    layer.msg(res.msg);
                                }

                            });
                        });


                    });

                }
            });
        });

    });


    exports('jie', null);
});